import json
import os

import requests
from openai import OpenAI


def education(argments):

    try:
        api_key = 'jRRhuCEvA4pih4hsfe3i252EYxHHBo'

        # 设置url
        url = f'https://www.apimy.cn/api/xxw/bgcx?key={api_key}'
        vcode = argments['location']

        # 发送post请求
        response = requests.post(url, data={'vcode': vcode})

        # 获取响应内容
        result = response.json()
        if result['code'] == 200:
            return json.dumps(result['data'], ensure_ascii=False)
        else:
            return f"学历验证失败，原因：{result['msg']}"
    except Exception as e:
        return f"学历验证失败，原因：{e}"

    # 打印结果
tools = [
    {
        "type": "function",
        "function": {
            "name": "education",
            "description": "根据学历验证码获取学历信息。例如:身份证号,手机号,姓名,地址等。",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "学历验证码",
                    }
                },
                "required": ["location"],
            },
        },
    },
]
client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

def get_response(messages):
    completion = client.chat.completions.create(
        model="qwen-plus",
        messages=messages,
        tools=tools,
    )
    return completion

def  main():

    user_input ="给我进行学历验证:dsfasfdsfs"
    ai_message=[{"role": "user", "content": user_input}]
    completion = get_response(ai_message)

    print(completion)

    ai_result=completion.choices[0].message
    print(ai_result)

    tools_call = completion.choices[0].message.tool_calls

    ai_message1={
         "role": "assistant",
         "content": ai_result.content,

         "tool_calls": [
             {
                 "id": tc.id,
                 "type":tc.type,
                 "function": {
                     "name": tc.function.name,
                     "Arguments":tc.function.arguments
                 }
             } for tc in tools_call or []
         ]
     }

    ai_message.append(ai_message1)

    if (ai_result.content =='' or ai_result.content is None) and tools_call is not None:
        for tool_call in tools_call:
            tool_name = tool_call.function.name
            toll_id=tool_call.id
            tool_arguments = tool_call.function.arguments
            result = education(json.loads(tool_arguments))
            if tool_name == 'education':
                ai_result2=education(result)
                ai_message1={
                    "role": "assistant",
                    "tool_call_id": toll_id,
                    "content": ai_result2,
                }
                ai_message.append(ai_message1)
                completion = get_response(ai_message)
                print(completion.choices[0].message.content)
            else:
                return "学历验证失败，原因：未知错误"

    else:

        print(ai_result.content)







